﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QuanLyKho.BL;
using QuanLyKho.Entity;
namespace QuanLyKho.GUI
{
    public partial class uNhapKho : DevExpress.XtraEditors.XtraUserControl
    {
        DataTable dt_NhapKho = new DataTable();
        DataTable dt_NhaCungCap;
        DataTable dtHangHoa = new DataTable();
        public uNhapKho()
        {
            InitializeComponent();

            dt_NhapKho.Columns.Add("maHang", typeof(String));
            dt_NhapKho.Columns.Add("tenHang", typeof(String));
            dt_NhapKho.Columns.Add("donGia", typeof(String));
            dt_NhapKho.Columns.Add("soLuong", typeof(String));
            dt_NhapKho.Columns.Add("thanhTien", typeof(String));
            gridControl1.DataSource = dt_NhapKho;
            bHangHoa bh=new bHangHoa();
            dtHangHoa = bh.getAllHangHoa();
            ItemSearchLookUpMaHang.DataSource = dtHangHoa;
            dt_NhapKho.ColumnChanging += new DataColumnChangeEventHandler(dt_NhapKho_ColumnChanging);
        }

        void dt_NhapKho_ColumnChanging(object sender, DataColumnChangeEventArgs e)
        {
           // MessageBox.Show(e.Column.ColumnName);
            if (e.Column.ColumnName == "donGia" && (e.Row["soLuong"].ToString() != "" ))
            {
                //MessageBox.Show("he");
                try
                {
                    int soluong1 = int.Parse(e.Row["soLuong"].ToString());
                    float dongia1 = float.Parse( e.ProposedValue.ToString());
                    float thanhTien = soluong1 * dongia1;
                    e.Row["thanhTien"] = thanhTien.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());

                }
            }else if (e.Column.ColumnName == "soLuong" && (e.Row["donGia"].ToString() != "") )
            {
                //MessageBox.Show("he");
                try
                {
                    int soluong1 = int.Parse(e.ProposedValue.ToString());
                    float dongia1 = float.Parse(e.Row["donGia"].ToString());
                    float thanhTien = soluong1 * dongia1;
                    e.Row["thanhTien"] = thanhTien.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());

                }
                
            }else if(e.Column.ColumnName == "maHang" )
            {
                //e.Row["tenHang"] = e.ProposedValue.ToString();
                DataRowView dv = timHangHoa(e.ProposedValue.ToString());
                e.Row["tenHang"] = dv["tenHang"];
            }

        }
        public DataRowView timHangHoa(String maHang)
        {
            DataView dv = dtHangHoa.DefaultView;
           // dv.RowFilter = "maHang='"+maHang+"'";
            dv.Sort = "maHang";
            return dv.FindRows(maHang)[0];
        }
        private void uNhapKho_Load(object sender, EventArgs e)
        {
            gridControl1.ShowOnlyPredefinedDetails = true;
            bNhaCungCap ncc = new bNhaCungCap();
            dt_NhaCungCap = ncc.getAllNhaCungCap();
            cbNhaCungCap.Properties.DataSource = dt_NhaCungCap;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //DataView dv = dtHangHoa.DefaultView;
            //dv.RowFilter = "maHang='hn001'";
            //dv.Sort = "maHang";
            //DataRowView dr =  dv.FindRows("hn001")[0];
            //MessageBox.Show(dr.Row[1].ToString());
            bNhapKho bnk = new bNhapKho();
            eNhapKho enk=new eNhapKho();
            bNhapKhoDetail bnkd = new bNhapKhoDetail();
            enk.ncId=int.Parse(txtMaNhaCungCap.Text);
            enk.ngayNhap = txtNgayNhap.Value ;
            int nkid  = bnk.insertNhapKho(enk);
            for (int i = 0; i < dt_NhapKho.Rows.Count; i++)
            {
                DataRow dr = (dt_NhapKho.Rows[i]);
                eNhapKhoDetail enkd = new eNhapKhoDetail();
                enkd.maHang = dr["maHang"].ToString();
                enkd.nkId = nkid;
                enkd.donGia = float.Parse(dr["donGia"].ToString());
                enkd.soLuong = int.Parse(dr["soLuong"].ToString());
                bnkd.insertNhapKhoDetail(enkd);
            }
            MessageBox.Show("Đã nhập kho thành công!");
        }

        private void cbNhaCungCap_EditValueChanged(object sender, EventArgs e)
        {

           //MessageBox.Show(e.ToString());
        }

        private void gridLookUpEdit1View_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
           // MessageBox.Show(e.RowHandle.ToString());
            DataRow dr = dt_NhaCungCap.Rows[e.RowHandle];
            txtDiaChi.Text = dr["diaChi"].ToString();
            txtMaNhaCungCap.Text = dr["ncId"].ToString();
            txtSoDt.Text = dr["soDienThoai"].ToString();
        }



        private void ItemSearchLookUpMaHang_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
          //  MessageBox.Show(e.NewValue.ToString());
         
        }

        private void ItemSearchLookUpMaHang_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e)
        {
           // MessageBox.Show(e.Value.ToString());
        }

    



    }
}
